package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.api.dto.GroundDTO;
import com.maiworld.api.vo.ResultVO;
import com.maiworld.mapper.NewsMapper;
import com.maiworld.mapper.TbGroundMapper;
import com.maiworld.mapper.TbSealSchoolMapper;
import com.maiworld.pojo.News;
import com.maiworld.pojo.TbGround;
import com.maiworld.pojo.TbGroundExample;
import com.maiworld.pojo.TbGroundExample.Criteria;
import com.maiworld.pojo.TbSealSchool;
import com.maiworld.seller.service.GroundService;

import entity.PageResult;

@Service
public class GroundServiceImpl implements GroundService {

	@Autowired
	private TbGroundMapper groundMapper;
	
	@Autowired
	private NewsMapper newsMapper;
	
	@Autowired
	private TbSealSchoolMapper tbSealSchoolMapper;
	
	/**
	 * 返回所有
	 */
//	@Override
//	public List<TbGround> findAll() {
//		
//		return groundMapper.selectByExample(null);
//	}
	/**
	 * 分页
	 */
//	@Override
//	public PageResult findPage(int pageNum, int pageSize) {
//		PageHelper.startPage(pageNum, pageSize);	//分页
//		Page<TbGround> page = (Page<TbGround>) groundMapper.selectByExample(null);
//		return new PageResult(page.getTotal(), page.getResult());
//	}
	/**
	 * 添加
	 */
	@Override
	public void add(TbGround ground) {
		ground.setCreated(new Date());
		groundMapper.insert(ground);

	}
	/**
	 * 修改
	 */
	@Override
	public void update(TbGround ground) {
		groundMapper.updateByPrimaryKey(ground);

	}
	/**
	 * 根据ID查询实体
	 */
	@Override
	public TbGround fingOne(Long id) {
		
		return groundMapper.selectByPrimaryKey(id);
	}
	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			groundMapper.deleteByPrimaryKey(id);
		}

	}

	@Override
	public PageResult pageFind(TbGround ground, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
			
			TbGroundExample example = new TbGroundExample();
			Criteria create = example.createCriteria();
			if(ground!=null){
				if(ground.getStatus()!=null && ground.getStatus().length()>0){
					create.andStatusEqualTo(ground.getStatus());
				}
				if(ground.getName()!=null && ground.getName().length()>0){					
					create.andNameLike("%"+ground.getName()+"%");
					
				}if(ground.getAddress()!=null && ground.getAddress().length()>0){					
					create.andAddressLike("%"+ground.getAddress()+"%");
				}
			}		
		Page<TbGround> page = (Page<TbGround>) groundMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	

	@Override
	public ResultVO<GroundDTO> findGround(String status) {
		ResultVO<GroundDTO> result = new ResultVO<>();
		GroundDTO tbGround = new GroundDTO();
			if(status.equals("")){
				result.setCode(0);
				result.setMsg("没有此分校信息");
				result.setData(null);
				return result;
		}
			TbGround selectByStatus = groundMapper.selectByStatus(status);
			if(selectByStatus==null){				
				result.setCode(0);
				result.setMsg("没有此分校的信息");
				result.setData(null);
				return result;
			}else {
				tbGround.setGround(selectByStatus);
				result.setCode(1);
				result.setMsg("有此分校的信息");
				result.setData(tbGround);
				return result;
			}
				
	}
	//关于我们的信息
	@Override
	public Map<String, Object> findGroou(String status) {
		Map<String, Object> result = new HashMap<String,Object>();
		
		TbGround a = groundMapper.selectByStatus(status);
		result.put("telephone", a.getTelephone());	//座机
		result.put("phone",a.getPhone());		//投诉电话
		result.put("address", a.getAddress());		//驾校地址

		List<News> news = newsMapper.selectByClassId(31L, status);
		Map<String, Object> res = new HashMap<String,Object>();
		res.put("id",news.get(0).getId());
		res.put("content",news.get(0).getContent());
		
		List<String> imgarr = new ArrayList<String>();
		if(news.get(0).getImgUrl1() != null){
			imgarr.add(news.get(0).getImgUrl1());
		}
		if(news.get(0).getImgUrl2() != null){
			imgarr.add(news.get(0).getImgUrl2());
		}
		if(news.get(0).getImgUrl3() != null){
			imgarr.add(news.get(0).getImgUrl3());
		}
		//System.out.println(news.get(0));
		res.put("imgUrl", imgarr);
		result.put("info", res);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}
	
	//报名首页获取驾校信息
	@Override
	public Map<String, Object> findGroundSid(Long sid, String status,String openid) {
		Map<String, Object> result = new HashMap<String,Object>();
		List<News> news = newsMapper.selectByClassId(31L, status);
		
		TbSealSchool seal = new TbSealSchool();
		seal = tbSealSchoolMapper.selectByPrimaryKey(Long.parseLong(status));
		String a = seal.getPhone();
		Map<String, Object> res = new HashMap<String,Object>();
		//res.put("phone", a);
		List<String> imgarr = new ArrayList<String>();
		if(news.get(0).getImgUrl1() != null){
			imgarr.add(news.get(0).getImgUrl1());
		}
		if(news.get(0).getImgUrl2() != null){
			imgarr.add(news.get(0).getImgUrl2());
		}
		if(news.get(0).getImgUrl3() != null){
			imgarr.add(news.get(0).getImgUrl3());
		}
		res.put("imgUrl", imgarr);
		result.put("bannerUrl", res);
		result.put("phone", a);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}
}
